package com.wtwd.device.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.model.vo.PageDataVO;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.AddHealthTypeContentDto;
import com.wtwd.device.model.entity.HealthTypeContent;
import com.wtwd.device.model.entity.UpdateHealthTypeContentDto;
import com.wtwd.device.model.vo.HealthTypeContentVo;
import com.wtwd.device.service.HealthTypeContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2023-08-16
 */
@Api(tags = "健康类型,贴士管理")
@RestController
@RequestMapping("/health-type-content")
public class HealthTypeContentController {

    @Resource
    private HealthTypeContentService healthTypeContentService;

    /**
     * 根据健康类型id,分页查询健康内容
     *
     * @param pageNo
     * @param pageSize
     * @param typeId
     * @return
     */
    @ApiOperation(value = "根据健康类型id,分页查询健康内容", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "typeId", value = "健康类型id", dataTypeClass = Integer.class, required = true)
    })
    @GetMapping("/queryPageByTypeId")
    public ResultVO<PageDataVO<HealthTypeContentVo>> queryPageByTypeId(@RequestParam(defaultValue = "1") Integer pageNo
            , @RequestParam(defaultValue = "10") Integer pageSize
            , @RequestParam(value = "typeId") Integer typeId) {
        Page<HealthTypeContent> page = new Page<>(pageNo, pageSize);
        return ResultVO.success(RespCode.SUCCESS, healthTypeContentService.queryPageByTypeId(page, typeId));
    }

    /**
     * 根据健康类型添加内容
     *
     * @param data
     * @return
     */
    @ApiOperation("新增贴士")
    @PostMapping("/add")
    public ResultVO add(@RequestBody AddHealthTypeContentDto data) {
        if (data == null || StringUtils.isBlank(data.getContent()) || data.getTypeId() == null) {
            return ResultVO.error(RespCode.NO_PARAM);
        }
        HealthTypeContent healthTypeContent = new HealthTypeContent();
        BeanUtils.copyProperties(data, healthTypeContent);
        boolean rest = false;
        try {
            rest = healthTypeContentService.insert(healthTypeContent);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rest) {
                return ResultVO.success(RespCode.SUCCESS);
            } else {
                return ResultVO.error(RespCode.FAILED);
            }
        }
    }

    /**
     * 修改健康内容根据内容主键id进行修改
     *
     * @param data
     * @return
     */
    @ApiOperation("修改贴士")
    @PostMapping("/update")
    public ResultVO update(@RequestBody UpdateHealthTypeContentDto data) {
        if (data == null || StringUtils.isBlank(data.getContent()) || data.getId() == null) {
            return ResultVO.error(RespCode.NO_PARAM);
        }
        HealthTypeContent healthTypeContent = new HealthTypeContent();
        BeanUtils.copyProperties(data, healthTypeContent);
        boolean rest = healthTypeContentService.updateHealthTypeContentById(healthTypeContent);
        if (rest) {
            return ResultVO.success(RespCode.SUCCESS);
        } else {
            return ResultVO.error(RespCode.FAILED);
        }
    }

    /**
     * 删除贴士
     *
     * @param id
     * @return
     */
    @ApiOperation("删除贴士")
    @ApiImplicitParam(name = "id", value = "贴士Id", required = true, dataTypeClass = Integer.class, paramType = "path")
    @DeleteMapping("/deleteById/{id}")
    public ResultVO deleteById(@PathVariable("id") Integer id) {
        boolean rest = healthTypeContentService.deleteById(id);
        if (rest) {
            return ResultVO.success(RespCode.SUCCESS);
        } else {
            return ResultVO.error(RespCode.FAILED);
        }
    }


}

